package com.qzsoft.zhjd.laboratory.modules.tensionRecording.controller;



import cn.hutool.json.JSONObject;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.qzsoft.jeemis.common.utils.Result;
import com.qzsoft.zhjd.common.utils.ResultUtils;
import com.qzsoft.zhjd.laboratory.modules.tensionProcess.entity.TensionProcess;
import com.qzsoft.zhjd.laboratory.modules.tensionRecording.entity.TensionRecording;
import com.qzsoft.zhjd.laboratory.modules.tensionRecording.service.TensionRecordingService;
import lombok.AllArgsConstructor;
import org.springframework.web.bind.annotation.*;

import java.util.Arrays;
import java.util.List;
import java.util.Map;
import java.util.UUID;

/**
 * 张拉记录(TensionRecording)表控制层
 *
 * @author makejava
 * @since 2023-07-12 13:41:44
 */
@RestController
@AllArgsConstructor
@RequestMapping("/laboratory/tensionRecording")
public class TensionRecordingController {

    private TensionRecordingService tensionRecordingService;

    /**
     * 分页
     *
     * @param params 查询条件
     * @return Result 分页列表
     */
    @GetMapping("page")
    public Result<IPage<TensionRecording>> page(@RequestParam Map<String, Object> params) {
        IPage<TensionRecording> page = tensionRecordingService.page(params);
        return ResultUtils.ok(page);
    }

    /**
     * 通过主键查询单条数据
     *
     * @param id 主键
     * @return 单条数据
     */
    @GetMapping("find/{id}")
    public Result selectOne(@PathVariable("id") String id) {
        return ResultUtils.ok(tensionRecordingService.getInfoById(id));
    }

    /**
     * 张拉整合信息接收接口
     * @param tensionJson
     * @return
     */
    @PostMapping("/tensioningDataReception")
    public Result tensioningDataReception(@RequestBody JSONObject tensionJson) {
         return tensionRecordingService.tensioningDataReception(tensionJson);
    }

    /**
     * 新增数据
     *
     * @param tensionRecording 实体对象
     * @return 新增结果
     */
    @PostMapping("/insert")
    public Result insert(@RequestBody TensionRecording tensionRecording) {
        if(tensionRecording.getCode()!=null){
            QueryWrapper<TensionRecording> queryWrapper = new QueryWrapper<TensionRecording>();
            queryWrapper.eq("code",tensionRecording.getCode());
            queryWrapper.eq("tension_time",tensionRecording.getTensionTime());
            List<TensionRecording> result = tensionRecordingService.list(queryWrapper);
            if(result.size()==0){
                tensionRecordingService.insertTensionRecording(tensionRecording);
            }
        }else{
            tensionRecordingService.insertTensionRecording(tensionRecording);
        }
        return ResultUtils.ok();
    }

    /**
     * 修改数据
     *
     * @param tensionRecording 实体对象
     * @return 修改结果
     */
    @PutMapping("/update")
    public Result update(@RequestBody TensionRecording tensionRecording) {
        tensionRecordingService.updateTensionRecordingById(tensionRecording);
        return ResultUtils.ok();
    }

    /**
     * 删除数据
     *
     * @param ids 主键结合
     * @return 删除结果
     */
    @DeleteMapping("/deleteByIds")
    public Result delete(@RequestBody String[] ids) {
        tensionRecordingService.deleteByIds(Arrays.asList(ids));
        return ResultUtils.ok();
    }
}

